<template>
  <div id="homePage">
    <!-- 图片搜索框 -->
    <div class="search-bar">
      <a-input-search
        v-model:value="searchParam.searchText"
        placeholder="全局搜索"
        enter-button="搜索"
        size="large"
        @search="doSearch"
      />
    </div>
    <!-- 分类和标签列表 -->
    <a-flex justify="space-between" align="center">
      <a-tabs v-model:activeKey="selectedCategory" @change="doSearch">
        <a-tab-pane key="all" tab="全部" />
        <a-tab-pane v-for="category in categoryList" :key="category" :tab="category" />
      </a-tabs>

      <a-form-item v-if="loginUser.id" label="我上传的图片" style="margin-top: 10px; margin-right: 10px">
        <a-switch v-model:checked="myPicture" @change="doSearch" />
      </a-form-item>
    </a-flex>

    <div class="tag-bar">
      <span style="margin-right: 8px">标签：</span>
      <a-space :size="[0, 8]" wrap>
        <a-checkable-tag
          v-for="(tag, index) in tagList"
          :key="tag"
          v-model:checked="selectedTagList[index]"
          @change="doSearch"
        >
          {{ tag }}
        </a-checkable-tag>
      </a-space>
    </div>

    <!-- 图片列表 -->
    <PictureList :dataList="dataList" :loading="loading" :show-op="false" />
    <!-- 分页组件 -->
    <a-pagination
      style="text-align: right"
      v-model:current="searchParam.current"
      v-model:pageSize="searchParam.pageSize"
      :total="total"
      @change="onPageChange"
    />
  </div>
</template>

<script setup lang="ts">
import { onMounted, reactive, ref } from 'vue'
import {
  listPictureTagCategoryUsingGet,
  listPictureVoByPageUsingPost,
} from '@/api/pictureController.ts'
import { message } from 'ant-design-vue'
import PictureList from '@/components/PictureList.vue'
import { useLoginUserStore } from '@/stores/useLoginUserStore.ts'

// 定义数据
const dataList = ref<API.UserVO[]>([])
const total = ref(0)
const loading = ref(true)

// 搜索条件
const searchParam = reactive<API.PictureQueryRequest>({
  current: 1,
  pageSize: 20,
  sortField: 'createTime',
  sortOrder: 'ascend',
})

// 获取登录用户信息
const loginUser = useLoginUserStore().loginUser

// 是否展示我的图片
const myPicture = ref<boolean>(false)

// 获取图片信息数据
const fetchPictureData = async () => {
  loading.value = true

  // 转换参数
  const param = {
    ...searchParam,
    tags: [] as string[],
  }
  if (myPicture.value) {
    param.userId = loginUser.id
  }
  if (selectedCategory.value !== 'all') {
    param.category = selectedCategory.value
  }
  selectedTagList.value.forEach((useTag, index) => {
    if (useTag) {
      param.tags?.push(tagList.value[index])
    }
  })

  // 请求图片数据
  const pictureData = await listPictureVoByPageUsingPost(param)
  if (pictureData.data.code === 0 && pictureData.data.data) {
    dataList.value = pictureData.data.data.records ?? []
    total.value = pictureData.data.data.total ?? 0
  } else {
    message.error('获取图片信息失败，' + pictureData.data.message)
  }
  loading.value = false
}

// 分页参数
const onPageChange = (page: number, pageSize: number) => {
  searchParam.current = page
  searchParam.pageSize = pageSize
  fetchPictureData()
}

// 搜索事件
const doSearch = () => {
  searchParam.current = 1
  fetchPictureData()
}

// 定义标签和分类选项变量
const categoryList = ref<string[]>([])
const selectedCategory = ref<string>('all')
const tagList = ref<string[]>([])
const selectedTagList = ref<boolean[]>([])

/**
 * 获取分类和标签选项
 * @param values
 */

const getTagCategoryOptions = async () => {
  try {
    const res = await listPictureTagCategoryUsingGet()
    // 请求成功
    if (res.data.code === 0 && res.data.data) {
      // 处理获取的数据
      tagList.value = res.data.data.tagList ?? []
      categoryList.value = res.data.data.categoryList ?? []
    } else {
      message.error('标签和分类列表获取失败，' + res.data.message)
    }
  } catch (e) {
    message.error('标签和分类列表获取失败，' + e)
  }
}

// 钩子函数，页面加载时获取数据
onMounted(() => {
  fetchPictureData()
  getTagCategoryOptions()
})
</script>

<style scoped>
#homePage {
  margin-bottom: 16px;
}

#homePage .search-bar {
  max-width: 480px;
  margin: 0 auto 16px;
}

#homePage .tag-bar {
  margin-bottom: 16px;
}
</style>
